import { GenID } from "../../utils";
import { SendMessageData } from "../sendMessageData";
import { baseInputVars, LastMsg } from "./baseInputVars";
import { BaseNode } from "./baseNode";
import { Annotation } from "@langchain/langgraph";

export class EndNode extends BaseNode {
    protected OnInit(): void {
    }
    name = "EndNode";
    inputVars = Annotation.Root({
        ...baseInputVars.spec,
        lastMsg: Annotation<LastMsg>,
    });
    async Run(): Promise<any> {
        await this.storeMsgSvr.AddToChatHistory({
            _id: GenID(),
            userID: this.inputVars.State.userID,
            convID: this.inputVars.State.convID,
            content: this.inputVars.State.lastMsg.content,
            contentType: this.inputVars.State.lastMsg.contentType,
            type: "ai",
            createdAt: new Date()
        });

        if (this.inputVars.State.lastMsg) {
            this.OnMessage?.(this, new SendMessageData(this.inputVars.State.convID, "last_full", "chat", { text: this.inputVars.State.lastMsg.content , contentType: this.inputVars.State.lastMsg.contentType }));
        }

        return this.outputVars.State;
    }
}